//
//  AOComposeController.swift
//  weibo_zha
//
//  Created by _zha on 16/11/4.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit
import SVProgressHUD

//发微博控制器
class AOComposeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        setupUI()
    }
    
    private func setupUI() {
        setupNavUI()
        view.addSubview(textView)
        //  添加控件设置约束
        textView.snp_makeConstraints { (make) in
            make.top.equalTo(view)
            make.leading.equalTo(view)
            make.trailing.equalTo(view)
            make.bottom.equalTo(view)
        }
    }
    
//      设置导航栏视图操作
    private func setupNavUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target:  self, action: #selector(cancelAction))
        navigationItem.titleView = titleLabel
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
        //  默认不可用
        navigationItem.rightBarButtonItem?.isEnabled = false
    }
    
    // MARK: - 点击导航栏按钮操作
    @objc private func sendAction() {
        //获取token
        let accessToken = AOUserAccountViewModel.sharedTools.accessToken!
        //获取微博内容
        let status = textView.text!
        
        SVProgressHUD.show()
        //
        AONetworkTools.sharedTools.update(access_token: accessToken, status: status, success: { (reponse) in
            SVProgressHUD.showSuccess(withStatus: "发送成功")
            }) { (error) in
                SVProgressHUD.showError(withStatus: "发送失败")
        }
    }
    
    @objc private func cancelAction()  {
        self.view.endEditing(true)
        dismiss(animated: true, completion: nil)
    }
    
    // MARK: - 懒加载控件
    fileprivate lazy var titleLabel: UILabel = {
        
        let label = UILabel()
        
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 17)
        label.textAlignment = .center
        //  指定多行显示
        label.numberOfLines = 0
        //  判断是name 是否为 nil
        if let name = AOUserAccountViewModel.sharedTools.userAccountModel?.screen_name {
            let title = "发微博\n" + name
            //  创建富文本
            let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: title)
            //  获取 name 的 rang
            let range = (title as NSString).range(of: name)
            
            attributedString.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12), NSForegroundColorAttributeName: UIColor.brown], range: range)
            
            label.attributedText = attributedString
        } else {
            label.text = "发微博"
        }
        label.sizeToFit()
        
        return label
    }()
    
    //  发送按钮
    fileprivate lazy var sendButton: UIButton = {
        let button = UIButton()
        // 添加点击事件
        button.addTarget(self, action: #selector(sendAction), for: .touchUpInside)
        //  设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_orange"), for: .normal)
        button.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .disabled)
        //  设置文字及颜色
        button.setTitle("发送", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.gray, for: .disabled)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //  设置大小
        button.size = CGSize(width: 45, height: 35)
        
        return button
    }()
    
    //  textView ,输入框
    fileprivate lazy var textView: AOComposeTextView = {
        let composeTextView = AOComposeTextView()
        composeTextView.placeHolder = "~~~~~来发微博啊~~~~~"
        composeTextView.font = UIFont.systemFont(ofSize: 14)
        //设置代理
        composeTextView.delegate = self
        return composeTextView
    }()
}


extension AOComposeController: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        navigationItem.rightBarButtonItem?.isEnabled = textView.hasText
    }
}
